package com.xgc.book.bookserver.util.exception;

import com.xgc.book.bookserver.util.ResultJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author PangJunjie
 * @Date 2023/11/14/014
 */
//@ResponseBody
//@ControllerAdvice
@RestControllerAdvice
@Slf4j
public class AppExceptionHandler {

    @ExceptionHandler(RTException.class)
    public ResultJson handlerRTException(RTException e) {
        log.info(e.getMsg());
        return ResultJson.error(e.getCode(), e.getMsg());
    }

    @ExceptionHandler(DuplicateKeyException.class)
    public ResultJson handlerDuplicateKeyException(DuplicateKeyException e) {
        log.info(e.getMessage());
        return ResultJson.error("主键重复！");
    }

    @ExceptionHandler(Exception.class)
    public ResultJson handlerException(Exception e) {
        log.info(e.getMessage());
        return ResultJson.error("服务器异常！");
    }

}
